<?php
/**
* 24tao前台明星操作
* author liwei
* date 2013-07-11
*/
class StarAction extends CommonAction {

	public function index(){
		Load('extend');
		import("ORG.Util.Page");
		$starId = $_GET['starid'];
		$Star = M('star');
		$starInfo = $Star->where("id = $starId")->select();
		//查询明星所对应标签
		$Tags = M('tags');
		$tagsInfo = $Tags->where("star_id=$starId")->select();
		//查询明星对应的所有商品
		$Goods = M('goods_base_info');
		$count = $Goods->where("goods_star_code=$starId")->count();
        $Page = new Page ($count , 12 );
        $show = $Page->showStar();
		if($count>12){
			$this->assign('page' , $show);
		}
        $goodsInfo = $Goods->limit( $Page->firstRow . ',' . $Page->listRows )->where("goods_star_code=$starId")->select();
		//评论最新数据提取
		$Comment = M('comment_info');
		$subQuery = $Comment->field('user_code,user_name,goods_code,content')->order('create_time desc')->select(false); 
		$commentInfo = $Comment->field('user_code,user_name,goods_code,content,count(1) as count')->table($subQuery.' a')->group('user_code,goods_code')->select();

		$_SESSION['publ'] = '';
		//瀑布流数据分离
		foreach($goodsInfo as $key=>$val){
			//查询出对应每个商品的评论
			foreach( $commentInfo as $k=>$v ){
				if( $val['goods_code'] == $v['goods_code'] ){
					$val['com_user_code'] = $v['user_code'];
					$val['com_user_name'] = $v['user_name'];
					$val['com_content'] = $v['content'];
					$val['com_count'] = $v['count']?$v['count']:0;
					unset($commentInfo[$k]);
				}
			}
			//第一次加载显示商品
			if( $key < 4 ){
				$fristData[$key] = $val;
			}
			//第二次加载商品记录
			if( $key >= 4 && $key < 8 ){
				$html .= '<div class="listBox">
						<div class="listCon white">
							<div class="bigPic">
								<a href="'.__APP__.'/Goods/index/goods_id/'.$val['goods_code'].'" target="_blank"><img src="/PUBLIC/Uploads/Goods/220_'.$val['goods_photo_add'].'" onmouseout="OnMouseOut('.$val['goods_code'].')" onmouseover="OnMouseOver('.$val['goods_code'].')" ></a>
								<div class="price"><em>¥&nbsp;</em>'.$val['goods_price'].'.00</div>
							</div>
							<div class="likeAndComment">
								<a href="" target="_blank" class="ilike"><b class="bg like-txt">喜欢</b><b class="like-num"><span>'.$val['goods_like_num'].'</span></b><i class="bg"></i></a><span class="comment"><a href="" target="_blank" class="bg title">评论</a><a href="" target="_blank" class="nick">'.$val['com_ount'].'</a></span>
							</div>
							<div class="info clearfix">
								<div class="userPic"><a href="" target="_blank"><img class="rank" src="__PUBLIC__/Uploads/Photo/'.$val['com_user_code'].'_64.jpg" width="30" height="30"></a></div>
								<div class="userDetail"><a href="" target="_blank" class="nick">'.$val['com_user_name'].'</a><span class="bg isBought"></span><br><a href="" target="_blank" class="c6">'.$val['com_content'].'</a></div>
							</div>
							<div class="addAbum" id="'.$val['goods_code'].'" onmouseout="OnMouseOut('.$val['goods_code'].')" onmouseover="OnMouseOver('.$val['goods_code'].')" onclick="addwardrobe('.$val['goods_code'].')"><i class="bg l"></i><span class="m">放入衣柜</span><i class="bg r"></i></div>
						</div>
						<div class="addzj">刚刚被淘友-<span>"<a href="" target="_blank" class="nick">美人鱼</a>"</span>-放入衣柜</div>
						<div class="bg btm"></div>
					</div>';	
			}
		}
		//将分页数据放入session
		$_SESSION['publ'] = $goodsInfo;

		$this->assign('goodsinfo',$fristData);
		$this->assign('html',$html);
		$this->assign('count',$count);
		$this->assign('tagsinfo',$tagsInfo);
		$this->assign('starinfo',$starInfo[0]);
		$this->display();
	}
	
	//瀑布流刷新数据
	public function ajaxPubl(){
		//$html ='';
		$starpagenum = $_GET['starpagenum'];
		$endpagenum = $_GET['endpagenum'];
		foreach($_SESSION['publ'] as $key=>$val){
			if( $key >= $starpagenum && $key < $endpagenum ){
				$html .= '<div class="listBox">
						<div class="listCon white">
							<div class="bigPic">
								<a href="'.__APP__.'/Goods/index/goods_id/'.$val['goods_code'].'" target="_blank"><img src="/PUBLIC/Uploads/Goods/220_'.$val['goods_photo_add'].'" onmouseout="OnMouseOut('.$val['goods_code'].')" onmouseover="OnMouseOver('.$val['goods_code'].')" ></a>
								<div class="price"><em>¥&nbsp;</em>'.$val['goods_price'].'.00</div>
							</div>
							<div class="likeAndComment">
								<a href="" target="_blank" class="ilike"><b class="bg like-txt">喜欢</b><b class="like-num"><span>'.$val['goods_like_num'].'</span></b><i class="bg"></i></a><span class="comment"><a href="" target="_blank" class="bg title">评论</a><a href="" target="_blank" class="nick">'.$val['com_ount'].'</a></span>
							</div>
							<div class="info clearfix">
								<div class="userPic"><a href="" target="_blank"><img class="rank" src="__PUBLIC__/Uploads/Photo/'.$val['com_user_code'].'_64.jpg" width="30" height="30"></a></div>
								<div class="userDetail"><a href="" target="_blank" class="nick">'.$val['com_user_name'].'</a><span class="bg isBought"></span><br><a href="" target="_blank" class="c6">'.$val['com_content'].'</a></div>
							</div>
							<div class="addAbum" id="'.$val['goods_code'].'" onmouseout="OnMouseOut('.$val['goods_code'].')" onmouseover="OnMouseOver('.$val['goods_code'].')" onclick="addwardrobe('.$val['goods_code'].')"><i class="bg l"></i><span class="m">放入衣柜</span><i class="bg r"></i></div>
						</div>
						<div class="addzj">刚刚被淘友-<span>"<a href="" target="_blank" class="nick">美人鱼</a>"</span>-放入衣柜</div>
						<div class="bg btm"></div>
					</div>';	
			}

		}
		$this->ajaxReturn($html);
	}

	public function ajaxTagGoods(){
		$starId = $_GET['starid'];
		$tagId = $_GET['tagid'];
		$Relation = M('tags_goods_relation');
		$tagsInfo = $Relation->where("tags_id=$tagId")->select();
		foreach($tagsInfo as $key=>$val){
			$goodIds .= $val['goods_id'].","; 
		}

		$Goods = M('goods_base_info');
		$condition['goods_star_code'] = $starId;
		if($tagId!='all'){
			$condition['goods_code'] = array('in',trim($goodIds,','));
		}
		$goodsInfo = $Goods->where($condition)->select();

		foreach($goodsInfo as $key=>$val){
			$html .= '<div class="listBox">
					<div class="listCon white">
						<div class="bigPic">
							<a href="'.__APP__.'/Goods/index/goods_id/'.$val['goods_code'].'" target="_blank"><img src="/PUBLIC/Uploads/Goods/220_'.$val['goods_photo_add'].'" onmouseout="OnMouseOut('.$val['goods_code'].')" onmouseover="OnMouseOver('.$val['goods_code'].')" ></a>
							<div class="price"><em>¥&nbsp;</em>'.$val['goods_price'].'.00</div>
						</div>
						<div class="likeAndComment">
							<a href="" target="_blank" class="ilike"><b class="bg like-txt">喜欢</b><b class="like-num"><span>'.$val['goods_like_num'].'</span></b><i class="bg"></i></a><span class="comment"><a href="" target="_blank" class="bg title">评论</a><a href="" target="_blank" class="nick">20</a></span>
						</div>
						<div class="info clearfix">
							<div class="userPic"><a href="" target="_blank"><img class="rank" src="http://wwc.taobaocdn.com/avatar/getAvatar.do?userId=51446130&amp;width=30&amp;height=30&amp;type=sns"></a></div>
							<div class="userDetail"><a href="" target="_blank" class="nick">huqian</a><span class="bg isBought"></span><br><a href="" target="_blank" class="c6">发货很快~宝贝也很好~绝对的好评~下次还会再来哒~</a></div>
						</div>
						<div class="addAbum" id="'.$val['goods_code'].'" onmouseout="OnMouseOut('.$val['goods_code'].')" onmouseover="OnMouseOver('.$val['goods_code'].')"  onclick="addwardrobe('.$val['goods_code'].')"><i class="bg l"></i><span class="m">放入衣柜</span><i class="bg r"></i></div>
					</div>
					<div class="addzj">刚刚被淘友-<span>"<a href="" target="_blank" class="nick">美人鱼</a>"</span>-放入衣柜</div>
					<div class="bg btm"></div>
				</div>';
		}

		$this->ajaxReturn($html);
	}

	function addWardRobe(){
		$uid = session(C('USER_AUTH_KEY'));
		$good_code = $_GET['id'];
		$Wardrobe = M('wardrobe_base_info');
		$WardrobeInfo = $Wardrobe->where("uid=$uid")->select();
		if( $WardrobeInfo ){
			$html = '<div class="select-wardrobe-form" style="display:block">
						<h1>请选择您的衣柜：</h1>
						<ul>';
						foreach($WardrobeInfo as $key=>$val){
							$html .= '<li><input type="radio" onclick=$("#radio").val("'.$val['id'].'") name="wardrobe" value="'.$val['id'].'" />'.$val['wardrobe_name'].'</li>';
						}
			$html .= 		'<li class="new-wardrobe"><span class="floatleft">&nbsp;&nbsp;&nbsp;新衣柜：</span></span><input type="text" name="" value="" class="wardrobe-name" /></li>
						</ul><input type="hidden" id="radio" value="" />
						<a href="javascript:;" class="w60r" title="提交" style="margin-left:18px;" onclick="saveWardrobe1('.$good_code.')">提交</a>
					</div>';
		}else{
			$html = '<div class="wardrobe-form">
						<h1>当前没有衣柜哦！请先添加</h1>
						<form action="" method="post">
							<span class="floatleft">衣柜名称：</span><input type="text" id="wardrobe" name="wardrobe" value="" class="wardrobe-name" /><a href="javascript:;" onclick="Wardrobe('.$good_code.')" class="w60r" title="提交">提交</a>
						</form>
					</div>';
		}

		$this->ajaxReturn($html);
	}

	function saveWardrobe(){
		$uid = session(C('USER_AUTH_KEY'));
		$robe_name = $_GET['robe_name'];
		$good_code = $_GET['good_code'];
		$id = $_GET['id'];
		$type = $_GET['type'];
		//新增衣柜加入
		if( $type == 0 ){
			$Wardrobe = M('wardrobe_base_info'); 
			if( !empty($robe_name) && !empty($good_code) ){
					$data['uid'] = $uid;
					$data['wardrobe_name'] = $robe_name;
					$data['createtime'] = date('Y-m-d H:i:s');
					if ( false !== $Wardrobe->add($data) ){
						$Relation = M('wardrobe_relation'); 
						$redata['wardrobe_id'] = $Wardrobe->getLastInsID();
						$redata['goods_id'] = $good_code;
						$redata['createtime'] = date('Y-m-d H:i:s');
						$Relation->add($redata);
						$this->ajaxReturn(true);
					} else {
						$this->ajaxReturn(false);
					}
				
			}
		}else{//已有衣柜加入
			$Relation = M('wardrobe_relation'); 
			$redata['wardrobe_id'] = $id;
			$redata['goods_id'] = $good_code;
			$redata['createtime'] = date('Y-m-d H:i:s');
			if ( false !== $Relation->add($redata) ){
				$this->ajaxReturn(true);
			} else {
				$this->ajaxReturn(false);
			}
		}
	}
}
